//
//  RBProcessOrderDetailVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/16.
//

#import "RBProcessOrderDetailVC.h"
#import "RBNavigationBar.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import "RBProcessOrderResponse.h"

@interface RBProcessOrderDetailVC ()

@property (nonatomic, strong) RBNavigationBar *navigationBar;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) RBProcessOrderDetailResponse *response;

@end

@implementation RBProcessOrderDetailVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self getData];
}

#pragma mark - Private

- (void)setup
{
    self.statusBarStyle = UIStatusBarStyleLightContent;
    self.statusBarStyle = UIStatusBarStyleLightContent;
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    [self.view view_addView:[RanTool imgViewWithMode:2 radius:0 imgName:@"processOrderDetailTopBg"] withHeight:Zoom(120)+kRBStatusBarHeight+kRBNavigationBarHeight width:kRBMainScreenWidth top:0 bottom:CGFLOAT_MIN left:0 right:0 centerX:0];
    self.navigationBar.hidden = NO;
}

- (void)fillData
{
    RBOrderDetailModel *model = self.response.data;
    [self.scrollView view_clear];
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        [view h_view_addView:[RanTool imgViewWithMode:0 radius:0 imgName:@"processOrderDetailLocation"] withHeight:Zoom(35) width:Zoom(36) top:CGFLOAT_MIN bottom:CGFLOAT_MIN left:0 right:0 centerY:YES];
        [view h_view_addView:({
            UIView *view = UIView.new;
            [view view_addView:[RanTool labelWithFontSize:Zoom(15) weight:2 color:UIColor.whiteColor align:0 text:@"邮寄地址"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(1) bottom:0 left:0 right:0 centerX:0];
            [view view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(12) weight:0 color:UIColor.whiteColor align:0 text:self.response.data.full_address];
                view.numberOfLines = 0;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(7) bottom:Zoom(1) left:0 right:0 centerX:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(15) right:0 centerY:NO];
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(25) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        [view view_addView:[RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:({
            NSString *orderNum = [NSString stringWithFormat:@"订单编号：%@", self.response.data.order_number];
            orderNum;
        })] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
        [view view_addView:({
            UIView *view = UIView.new;
            [view h_view_addView:[RanTool imgViewWithMode:2 radius:0 imgName:self.response.data.file] withHeight:Zoom(71) width:Zoom(71) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:0];
            [view h_view_addView:({
                UILabel *view = [RanTool labelWithFontSize:Zoom(13) weight:2 color:[RBThemeConfig textBlackColor] align:0 text:self.response.data.goods_name];
                view.numberOfLines = 2;
                view;
            }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(7) bottom:CGFLOAT_MIN left:Zoom(15) right:0 centerY:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(14) bottom:CGFLOAT_MIN left:Zoom(13) right:Zoom(13) centerX:0];
        view;
    }) withHeight:Zoom(123) width:CGFLOAT_MIN top:Zoom(25) bottom:0 left:Zoom(15) right:Zoom(15) centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        CGFloat padding = Zoom(18);
        [view view_addView:[self itemTitleWithTitle:@"基础信息"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"医疗机构名称" des:self.response.data.institution_name blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"医疗机构地址" des:self.response.data.institution_address blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"医生姓名" des:self.response.data.doctor_name blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"手机号" des:self.response.data.doctor_mobile blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"患者姓名" des:self.response.data.patient_name blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"患者性别" des:self.response.data.patient_sex blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        [view view_addView:[self itemWithTitle:@"医生附言" des:self.response.data.postscript blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(15) right:Zoom(15) centerX:0];
    [self.scrollView addView:({
        UIView *view = UIView.new;
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCornerRadius:Zoom(8)];
        CGFloat padding = Zoom(18);
        [view view_addView:[self itemTitleWithTitle:@"产品规格明细"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:padding left:0 right:0 centerX:0];
        for (RBCartSpecModel *model in self.response.data.spec_key_name) {
            NSString *leftString = model.key;
            NSString *rightString = [model.value componentsJoinedByString:@"　"];
            NSInteger index = [self.response.data.spec_key_name indexOfObject:model];
            [view view_addView:[self itemWithTitle:leftString des:rightString blue:(index != 0)] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
        }
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(10) left:Zoom(15) right:Zoom(15) centerX:0];
    
    if (![self.response.data.order_status isEqualToString:@"0"]) {
        [self.scrollView addView:({
            UIView *view = UIView.new;
            view.backgroundColor = UIColor.whiteColor;
            [view rbSetCornerRadius:Zoom(8)];
            CGFloat padding = Zoom(18);
            [view view_addView:[self itemTitleWithTitle:@"订单信息"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(13) bottom:padding left:0 right:0 centerX:0];
            [view view_addView:[self itemWithTitle:@"物流公司" des:model.kuaidi_company_name blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
            [view view_addView:[self itemWithTitle:@"快递单号" des:model.kuaidi_order_num blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
            [view view_addView:[self itemWithTitle:@"创建时间" des:model.create_time blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
            [view view_addView:[self itemWithTitle:@"发货时间" des:model.shipping_time blue:NO] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:padding bottom:padding left:0 right:0 centerX:0];
            view;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(10) bottom:Zoom(30) left:Zoom(15) right:Zoom(15) centerX:0];
    }
}

#pragma mark - Network

- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"order_id"] = self.order_id;
    } path:_Order_detail post:YES log:YES responseClass:RBProcessOrderDetailResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(id data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBProcessOrderDetailResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event

#pragma mark - Setter

#pragma mark - Getter

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *view = RBNavigationBar.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight+kRBNavigationBarHeight);
            }];
            view.needGradientView = NO;
            
            [view.naviBar addBackBtn];
            [view.naviBar view_addView:[RanTool labelWithFontSize:Zoom(18) weight:1 color:UIColor.whiteColor align:1 text:@"加工订单详情"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:CGFLOAT_MIN right:CGFLOAT_MIN centerX:YES];
            
            view;
        });
    }
    return _navigationBar;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.navigationBar.mas_bottom);
            }];
            view;
        });
    }
    return _scrollView;
}

- (UIView *)itemTitleWithTitle: (NSString *)title
{
    UIView *view = UIView.new;
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(13) weight:1 color:[RBThemeConfig textBlackColor] align:0 text:title];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Zoom(13));
        make.top.bottom.mas_equalTo(0);
    }];
    return view;
}

- (UIView *)itemWithTitle: (NSString *)title des: (NSString *)des blue: (BOOL)blue
{
    UIView *view = UIView.new;
    
    UILabel *titleL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textGrayColor] align:0 text:title];
    [titleL addPriority:1];
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(Zoom(15));
    }];
    
    if (!des.length)
    {
        des = @"　";
    }
    UILabel *desL = [RanTool labelWithFontSize:Zoom(13) weight:0 color:[RBThemeConfig textBlackColor] align:0 text:des];
    if (blue)
    {
        UIFont *font = [UIFont systemFontOfSize:Zoom(13)];
        desL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
            [maker addAttributeStringWithFont:font color:[UIColor colorWithHexString:@"#3974EC"] string:des];
            [maker addAttributeStringWithFont:font color:[UIColor colorWithHexString:@"#3974EC"] string:@" "];
//            [maker addAttributeStringWithImage:[UIImage imageNamed:@"processOrderDetailDown"] imageSize:CGSizeMake(Zoom(9), Zoom(5)) maxFont:font];
        }];
    }
    
    desL.numberOfLines = 0;
    desL.textAlignment = NSTextAlignmentRight;
    [view addSubview:desL];
    [desL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-Zoom(15));
        make.left.mas_equalTo(titleL.mas_right).offset(Zoom(15));
    }];
    
    return view;
}



@end
